package com.frejoys.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.frejoys.common.aop.Authority;
import com.frejoys.common.aop.Authorization;
import com.frejoys.common.constant.CommonConstant;
import com.frejoys.common.dto.admin.OrderSnAndPageDto;
import com.frejoys.common.dto.admin.StoreIdAndOrderDto;
import com.frejoys.common.dto.admin.StoreIdAndPageDto;
import com.frejoys.common.dto.common.IdDto;
import com.frejoys.common.dto.common.OrderSnDto;
import com.frejoys.common.error.HttpStatus;
import com.frejoys.common.exception.GeneralException;
import com.frejoys.common.dto.common.IdDto;
import com.frejoys.common.enums.EVerify;
import com.frejoys.common.error.HttpStatus;
import com.frejoys.common.exception.GeneralException;
import com.frejoys.common.response.Result;
import com.frejoys.common.util.CacheKeyUtil;
import com.frejoys.common.util.CacheKeyUtil;
import com.frejoys.common.util.RequestUtil;
import com.frejoys.common.vo.admin.OrderGoodsListVo;
import com.frejoys.common.vo.admin.OrderListVo;
import com.frejoys.common.vo.admin.StoreActivityQueueListVo;
import com.frejoys.dao.entity.Order;
import com.frejoys.dao.entity.StoreActivityQueue;
import com.frejoys.service.*;
import com.frejoys.dao.entity.Order;
import com.frejoys.service.OrderGoodsService;
import com.frejoys.service.OrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "后台订单管理", description = "后台订单管理")
@RestController
@RequiredArgsConstructor
@RequestMapping(CommonConstant.routePrefixAdmin)
@Authorization(Authority.Private)
public class AdminOrderController {

    private final OrderService orderService;

    private final OrderGoodsService orderGoodsService;

    private final RedissonClient redissonClient;

    @PostMapping("/order/orderListInfo")
    @Operation(summary = "查询订单列表")
    public Result<IPage<OrderListVo>> orderListInfo(@RequestBody @Validated StoreIdAndOrderDto dto) {
        return Result.success(orderService.orderListInfo(dto));
    }


    @PostMapping("/order/orderGoodsListInfo")
    @Operation(summary = "查询订单商品列表")
    public Result<IPage<OrderGoodsListVo>> orderGoodsListInfo(@RequestBody @Validated OrderSnAndPageDto dto) {
        return Result.success(orderGoodsService.orderGoodsListInfo(dto));
    }

    @Operation(summary = "管理员订单退款 - 未核销")
    @PostMapping("/order/refund")
    public Result orderRefund(@RequestBody @Validated IdDto dto){

        Long userId = orderService.userIdByOrderId(dto.getId());

        RLock lock = redissonClient.getLock(CacheKeyUtil.orderRefundLockCacheKey(userId));

        if (lock.tryLock()) {
            try {

                orderService.orderRefund(userId, dto.getId());

            } finally {
                if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                    lock.unlock();
                }
            }
        } else {
            throw new GeneralException(HttpStatus.beingProcessed);
        }

        return Result.success();
    }


}
